<?php
namespace app\index\controller;
use app\admin\model\Teacher;
use app\index\model\LabApp;
use think\Controller;
use think\Session;
use think\Validate;

Class Lab extends Controller {
    protected $middleware = [
        'UserCheck'
    ];
	//空课室预约首页
	public function lab_empty(){
        $after_date=date('Y-m-d');
        $data=input('get.');
        if (isset($data['time'])){
            $validate=new Validate();
	        $rule=[
	            'date|时间'=>"require|dateFormat:Y-m-d|after:$after_date",
                'time|时间段'=>'require',
            ];
	        $va=$validate->check($data,$rule);
	        if ($va==true){
	            $lab_app=new LabApp();
	            $lab_app=$lab_app->where([
	                'date' =>$data['date'],
                    'time' =>$data['time'],
                ])->where('statu','neq','2')->column('lab_id');
	            $lab=new \app\admin\model\Lab();
	            $lab=$lab
                    ->where('id','notin',$lab_app)
                    ->paginate(10,false,['query'=>$data]);
	            $empty='暂无空闲课室';
	            $date=$data['date'];
	            $time=$data['time'];
            }else{
                $this->error($validate->getError());
            }
        }else{
	        $lab=[] ;
	        $date='';
	        $time='';
	        $empty='请选择时间段';
        }
	    $this->assign('labs',$lab);
	    $this->assign('empty',$empty);
	    $this->assign('date',$date);
	    $this->assign('time',$time);
		return $this->fetch();
	}
	//课室预约
    public function lab_app(){
	    $date=input('post.');
	    $lab_app=new LabApp();
        $lab_find=$lab_app->where([
            'lab_id'=>$date['lab_id'],
            'date'   =>$date['date'],
            'time'   =>$date['time'],
        ])->where('statu','neq',2)->find();
        if ($lab_find){
            $msg=$this->msg(400,'此时段的课室已被预约请重新预约');
        }else{
            $lab_yuyue=$lab_app->save([
                'lab_id'=>$date['lab_id'],
                'user_id'=>\session('user')['id'],
                'date'   =>$date['date'],
                'time'   =>$date['time'],
                'statu'  =>0
            ]);
            if ($lab_yuyue){
                $msg=$this->msg(200,'预约成功，请等待审批');
                $this->lab_app_send($date['lab_id'],$date['date'].' '.$date['time']);
            }else{
                $msg=$this->msg(400,'预约失败');
            }
        }
	    return $msg;
    }
    //预约邮件提醒功能
    public function lab_app_send($lab_id,$datetime){
	    $lab=new \app\admin\model\Lab();
	    $teacher=new Teacher();
	    $lab=$lab->where('id',$lab_id)->find();
	    $lab_name=$lab['name'];
	    $teacher=$teacher->where('id',$lab['teacher_id'])->find();
	    $teacher_email=$teacher['email'];
	    $mail=new Mail();
	    $mail=$mail
            ->send_mail(
                $teacher_email,
                '您有一个预约',
                "您管理的实验室$lab_name,在$datetime 被申请预约，请前去审核。");
    }
    //当前预约记录
    public function lab_app_list(){
	    $lab_app=new LabApp();
	    $user_id=\session('user')['id'];
	    $lab_app=$lab_app
            ->with('lab')
            ->where([
                'user_id'=>$user_id,
                'statu'  =>0,
            ])->paginate(10);
	    $this->assign('lab_apps',$lab_app);
	    return $this->fetch();
    }
    //历史预约记录
    public function lab_history_list(){
        $lab_app=new LabApp();
        $user_id=\session('user')['id'];
        $lab_app=$lab_app
            ->with('lab')
            ->where([
                'user_id'=>$user_id,
            ])->where('statu','neq',0)
            ->order('create_time','desc')
            ->paginate(10);
        $this->assign('lab_apps',$lab_app);
	    return $this->fetch();
    }
    //信息
    private function msg($statu,$msg){
	    return [
	        'statu' =>$statu,
            'msg'   =>$msg
        ];
    }
}